package com.ruoyi.common.utils;

import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/***
 * 二维码生成
 * **/
public class QRCodeUtils {

    public final static Integer DEFAULT_QRCODE_WIDTH = 200;
    public final static Integer DEFAULT_QRCODE_HEIGHT = 200;
    private final static String DEFAULT_FILE_EXT = "png";

//    public static void main(String[] args) throws Exception {
//        String url = "https://www.baidu.com";  //这里设置自定义网站url或文字
//        //erweima(url);
//        //code(url, "/Users/daniel/Desktop/vue/index/");
//
//        //System.out.printf(getQRCodeBase64(url));
//    }


    public static String getQRCodeImg(String url, String path, Integer width, Integer height) {
        try {
            String codeName = UUID.randomUUID().toString();// 二维码的图片名
            BitMatrix bitMatrix = getBitMatrix(url, width, height);
            String fileName = String.format("%s.%s", codeName, DEFAULT_FILE_EXT);
            File file1 = new File(path, fileName);
            MatrixToImageWriter.writeToFile(bitMatrix, DEFAULT_FILE_EXT, file1);
            return fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getQRCodeBase64(String url) {
        return getQRCodeBase64(url, DEFAULT_QRCODE_WIDTH, DEFAULT_QRCODE_HEIGHT);
    }

    public static String imageToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String base64 = Base64.getEncoder().encodeToString(data);
        return base64;
    }

    public static String getQRCodeBase64(String url, Integer width, Integer height) {
        try {
            BitMatrix bitMatrix = getBitMatrix(url, width, height);

            //字节数组输出流
            ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(bitMatrix, DEFAULT_FILE_EXT, imageOut);

            //字节数组输入流
            ByteArrayInputStream imageIn = new ByteArrayInputStream(imageOut.toByteArray());
            BufferedImage bImage = ImageIO.read(imageIn);

            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ImageIO.write(bImage, DEFAULT_FILE_EXT, bs);

            String base64 = Base64.getEncoder().encodeToString(bs.toByteArray());
            return base64;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;

    }

    public static BufferedImage getQRCodeImage(String url, Integer width, Integer height,boolean hasBorder) {
        try {
            BitMatrix matrix = getBitMatrix(url, width, height);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, matrix.get(x, y) ? 0x00000000 : 0xFFFFFF);
                }
            }
            if(!hasBorder){
                return image;
            }
// 设置边框宽度
            int borderWidth = 2;
            int width_img = width - borderWidth;
            int height_img = width - borderWidth;

// 创建新的图片
            BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = newImage.getGraphics();
// 绘制原始图片
            g.drawImage(image, 1, 1, null);
// 绘制边框
            g.setColor(Color.yellow);
            g.drawRect(1, 1, width_img, height_img);
            return newImage;
//            String fileName = String.format("%s.%s", "asddsd", DEFAULT_FILE_EXT);
//            File file1 = new File("/Users/daniel/Downloads/", fileName);
//            ImageIO.write(newImage, "png", file1);
            // MatrixToImageWriter.writeToFile(matrix, DEFAULT_FILE_EXT, file1);

//            //字节数组输出流
//            ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
//            MatrixToImageWriter.writeToStream(matrix, DEFAULT_FILE_EXT, imageOut);
//
//            //字节数组输入流
//            ByteArrayInputStream imageIn = new ByteArrayInputStream(imageOut.toByteArray());
//            BufferedImage bImage = ImageIO.read(imageIn);
//
//            ByteArrayOutputStream bs = new ByteArrayOutputStream();
//            ImageIO.write(bImage, DEFAULT_FILE_EXT, bs);
//
//            String base64 = Base64.getEncoder().encodeToString(bs.toByteArray());
            //  return base64;
          //  return "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;

    }

    private static BitMatrix getBitMatrix(String url, Integer width, Integer height) throws WriterException {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, "3");
        BitMatrix bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, width, height, hints);
        return bitMatrix;
    }

//    public static void main(String[] args) {
//        getQRCodeImage("www.baidu.com", 200, 200);
//    }
}
